package com.zhu.transaction.controller;

import com.zhu.transaction.mapper.OrderMapper;
import com.zhu.transaction.mapper.ProductMapper;
import com.zhu.transaction.mapper.UserMapper;
import com.zhu.transaction.pojo.reports.OrderReports;
import com.zhu.transaction.pojo.reports.ProductReports;
import com.zhu.transaction.pojo.reports.UserReports;
import com.zhu.transaction.pojo.state.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author byosoft
 * @createtime 2021/3/11-17:55
 */
@RestController
@RequestMapping("/reports")
public class ReportsController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    OrderMapper orderMapper;

    @GetMapping("/user")
    public R getUserReposts() {
        //查出所有数据
        List<UserReports> userReports = userMapper.queryUserReports();
        List<String> timeList = new ArrayList<>();
        List<Integer> newAddUserCountList = new ArrayList<>();
        List<Integer> curAllUserCountList = new ArrayList<>();
        for (int i = 0; i < userReports.size(); i++) {
            UserReports reports = userReports.get(i);
            timeList.add(reports.getTime());
            newAddUserCountList.add(reports.getUserCount());
            if (i == 0) {
                curAllUserCountList.add(reports.getUserCount());
            } else {
                curAllUserCountList.add(curAllUserCountList.get(i - 1) + reports.getUserCount());
            }
        }
        return R.ok().data("time", timeList).data("newUserList", newAddUserCountList).data("allUserList", curAllUserCountList);
    }


    @GetMapping("/product/new")
    public R getNewReportsReposts() {
        //查出所有数据
        List<ProductReports> productReports = productMapper.queryNewProductReports();
        List<String> timeList = new ArrayList<>();
        List<Integer> newAddProductCountList = new ArrayList<>();

        for (ProductReports productReport : productReports) {
            timeList.add(productReport.getTime());
            newAddProductCountList.add(productReport.getNewProductCount());
        }

        return R.ok().data("time", timeList).data("newProductList", newAddProductCountList);
    }

    @GetMapping("/product/category")
    public R getCategoryProductReposts() {
        //查出所有数据
        List<ProductReports> productReports = productMapper.queryCategoryProductReports();
        List<String> categoryList = new ArrayList<>();
        List<Integer> curCategoryProductCountList = new ArrayList<>();
        for (ProductReports productReport : productReports) {
            categoryList.add(productReport.getCategoryName());
            curCategoryProductCountList.add(productReport.getCurCategoryProductCount());
        }
        return R.ok().data("curCategoryProductCountList", curCategoryProductCountList).data("categoryList", categoryList);
    }


    @GetMapping("/order/new")
    public R getNesOrderReposts() {
        //查出所有数据
        List<OrderReports> orderReports = orderMapper.queryNewOrderReports();
        List<String> timeList = new ArrayList<>();
        List<Integer> newOrderCountList = new ArrayList<>();
        List<Integer> curAllOrderCountList = new ArrayList<>();
        for (int i = 0; i < orderReports.size(); i++) {
            OrderReports reports = orderReports.get(i);
            timeList.add(reports.getTime());
            newOrderCountList.add(reports.getNewAddOrderCount());
            if (i == 0) {
                curAllOrderCountList.add(reports.getNewAddOrderCount());
            } else {
                curAllOrderCountList.add(curAllOrderCountList.get(i - 1) + reports.getNewAddOrderCount());
            }
        }
        return R.ok().data("curOrderCountList", curAllOrderCountList).data("newOrderCountList", newOrderCountList).data("timeList", timeList);
    }

    @GetMapping("/order/state")
    public R getOrderStateReposts() {
        //查出所有数据
        List<OrderReports> orderReports = orderMapper.queryOrderStateReports();
        List<String> orderStateList = new ArrayList<>();
        List<Integer> stateCountList = new ArrayList<>();
        for (OrderReports productReport : orderReports) {
            orderStateList.add(productReport.getOrderState());
            stateCountList.add(productReport.getOrderStateCount());
        }
        return R.ok().data("orderStateList", orderStateList).data("stateCountList", stateCountList);
    }

    @GetMapping("/order/payWay")
    public R getPayWayReposts() {
        //查出所有数据
        List<OrderReports> orderReports = orderMapper.queryPayWayReports();
        List<String> payWayList = new ArrayList<>();
        List<Integer> PayWayCountList = new ArrayList<>();
        for (OrderReports productReport : orderReports) {
            payWayList.add(productReport.getPayWay());
            PayWayCountList.add(productReport.getPayWayCount());
        }
        return R.ok().data("payWayList", payWayList).data("PayWayCountList", PayWayCountList);
    }


}
